package com.shiyi.suanfa;

import java.util.*;

/**
 * 猜数字
 */
public class demo2 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String S = in.nextLine();
            int count = Integer.parseInt(S);
            ArrayList<String> list = new ArrayList<>();
            HashMap<String, String> map = new HashMap<>();
            for (int i = 0; i < count; i++) {
                String string = in.nextLine();
                String[] split = string.split(" ");
                map.put(split[0],split[1]);
            }

            for (int i = 0; i < 9999; i++) {
                String format = String.format("%04d", i);
                boolean aflag = true;
                boolean bflag = true;
                if (format.equals("3585")){
                    System.out.println();
                }

                for (Map.Entry<String, String> entry : map.entrySet()) {
                    String value = entry.getValue();
                    String key = entry.getKey();

                    ArrayList<Character> list1 = new ArrayList<>();
                    for (int j = 0; j < 4; j++) {
                        list1.add(format.charAt(j));
                    }

                    char a = value.charAt(0);  // A
                    char b = value.charAt(2);  // B

                    // 对比A规则
                    int yes = 0;
                    for (int j = 0; j < 4; j++) {
                        if (key.charAt(j) == format.charAt(j)){
                            yes++;
                        }
                    }
                    String s = String.valueOf(a);
                    if (yes==Integer.parseInt(s)){ // A全通过
                        int contain = 0;
                        for (int j = 0; j < 4; j++) {
                            char c = key.charAt(j);
                            if (list1.contains(c)){
                                contain++;
                                boolean remove = list1.remove(Character.valueOf(c));
                            }
                        }
                        for (int j = 0; j < 4; j++) {
                            if ( key.charAt(j) == format.charAt(j)){
                                contain--;
                            }

                        }
                        // B,的数量匹配
                        if (contain==Integer.parseInt(String.valueOf(b))){

                        }else {
                            bflag = false;
                            break;
                        }
                    }else {
                        aflag = false;
                        break;
                    }
                }
                if (bflag && aflag){
                    list.add(format);
                }
            }
            if (list.size() == 1){
                System.out.println(list.get(0));
            }else {
                System.out.println("N/A");
            }
        }
    }
}
